package com.zzk.lanouhn.myapplication1.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.RelationGoodsAdapter;
import com.zzk.lanouhn.myapplication1.adapters.UserIconAdapter;
import com.zzk.lanouhn.myapplication1.modles.CollectSingleGood;
import com.zzk.lanouhn.myapplication1.modles.RelationGoods;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;
import com.zzk.lanouhn.myapplication1.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者个人信息->收藏->单品二级页面
 * Created by lanouhn on 16/9/15.
 */
public class AuthorCollectSingleGoodActivity extends AppCompatActivity {
    private ImageView iv_back, iv_pic, iv_share, iv_more;
    private TextView tv_title, tv_price, tv_desc, tv_likes;
    private CollectSingleGood singleGood;
    private List<CollectSingleGood.DataBean.LikesListBean> likelist = new ArrayList<>();
    private List<RelationGoods.DataBean.ProductBean> relationGoods = new ArrayList<>();
    private UserIconAdapter adapter;
    private RelationGoodsAdapter relationAdapter;
    private RecyclerView iconView;
    private MyGridView relationGoodsView;
    private String singleGoodId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author_single_good);
        initView();
        initData();
        initEvent();
        getData();
    }

    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back_collect_single_good);
        iv_share = (ImageView) findViewById(R.id.iv_share_collect_single_good);
        iv_pic = (ImageView) findViewById(R.id.iv_pic_single_good_author);
        iv_more = (ImageView) findViewById(R.id.iv_check_like_user);
        tv_title = (TextView) findViewById(R.id.title_act_shopetails_author);
        tv_price = (TextView) findViewById(R.id.tv_price_singlegood_author);
        tv_desc = (TextView) findViewById(R.id.content_act_shopetails_author);
        tv_likes = (TextView) findViewById(R.id.tv_likes_singlegood_author);
        iconView = (RecyclerView) findViewById(R.id.recy_icon_like_user);
        relationGoodsView = (MyGridView) findViewById(R.id.gv_relation_goods_author);

    }

    private void initData() {
        Bundle bundle = this.getIntent().getExtras();
        singleGoodId = bundle.getString("authorSingle");
        SharedPreferences preferences = this.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("singleGoodId", singleGoodId);
        editor.commit();
    }

    private void initEvent() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        iv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AuthorCollectSingleGoodActivity.this, NewestLikeActivity.class);
                startActivity(intent);
            }
        });
        relationGoodsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(AuthorCollectSingleGoodActivity.this,AuthorCollectSingleGoodActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("authorSingle",relationGoods.get(position).getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void getData() {
        String url1 = "http://open4.bantangapp.com/product/newInfo?";
        String dataUrl1 = "app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2" +
                "&screensize=720" +
                "&v=19&id=" + singleGoodId + "&trace_id=&page=0" +
                "&pagesize=10&comments_pagesize=10";
        try {
            HttpUtils.doPostAsyn(url1, dataUrl1, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson1 = new Gson();
                    singleGood = gson1.fromJson(result, CollectSingleGood.class);
                    likelist = gson1.fromJson(result, CollectSingleGood.class).getData().getLikes_list();
                    if (!singleGood.getData().equals("")) {
                        handler.sendEmptyMessage(1);
                    }
                    if (likelist.size() > 0) {
                        handler.sendEmptyMessage(2);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url2 = "http://open4.bantangapp.com/product/relation?";
        String dataUrl2 = "app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2&screensize=720&v=19" +
                "&object_id=" + singleGoodId + "&type_id=2";
        try {
            HttpUtils.doPostAsyn(url2, dataUrl2, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson2 = new Gson();
                    relationGoods = gson2.fromJson(result, RelationGoods.class).getData().getProduct();
                    if (relationGoods.size() > 0) {
                        handler.sendEmptyMessage(3);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    tv_title.setText(singleGood.getData().getProduct().getTitle());
                    tv_desc.setText(singleGood.getData().getProduct().getDesc());
                    tv_likes.setText(singleGood.getData().getLikes() + "人喜欢");
                    tv_price.setText("￥" + singleGood.getData().getProduct().getPrice());
                    Picasso.with(AuthorCollectSingleGoodActivity.this)
                            .load(singleGood.getData()
                                    .getProduct()
                                    .getPic().get(0)
                                    .getPic()).into(iv_pic);
                    break;
                case 2:
                    adapter = new UserIconAdapter(AuthorCollectSingleGoodActivity.this, likelist);
                    iconView.setAdapter(adapter);
                    LinearLayoutManager manager = new LinearLayoutManager(AuthorCollectSingleGoodActivity.this);
                    manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    iconView.setLayoutManager(manager);
                    break;
                case 3:
                    relationAdapter = new RelationGoodsAdapter(AuthorCollectSingleGoodActivity.this, relationGoods);
                    relationGoodsView.setAdapter(relationAdapter);
                    break;
                default:

                    break;
            }
        }
    };
}
